package org.example;

import java.io.IOException;
import java.io.InputStream;

//把请求信息封装为Request对象（根据InputSteam输入流封装）
public class Request {
    private String method; // 请求方式，比如GET/POST
    private String url;  // 例如 /,/index.html

    private InputStream inputStream;  // 输入流，其他属性从输入流中解析出来

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int len = 0;
        while (len == 0) {
            len = inputStream.available();
        }

        byte[] b = new byte[len];
        inputStream.read(b);
        String s = new String(b);
        String[] split = s.split("\n");
        String s1 = split[0];
        String[] s2 = s1.split(" ");
        method = s2[0].trim();
        url = s2[1].trim();
    }


    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public String toString() {
        return "Request{" +
                "method='" + method + '\'' +
                ", url='" + url + '\'' +
                ", inputStream=" + inputStream +
                '}';
    }
}
